/***************************************************************************
 *   Copyright (C) 2009 2010 by Lasse Liehu                                *
 *   lliehu@kolumbus.fi                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

/* Settings cleaner for Google Reader Notifier Qt
   
   This application is a utility for removing Google Reader Notifier Qt's
   user-specific settings from the system.
   
   USAGE:
   Without arguments it will show a graphical confirmation before deleting
   the settings.

   If run with --silent, it will simply delete the settings silently (no
   graphical or command line output other than a graphical error message
   when invalid parameters were passed).

   Also, if run with --temp-unset-autostart-only, it will only silently
   unset autostart and request the app to set it back on the next time it's
   started. Won't delete settings if run with this argument. This option
   isn't usually needed.
*/

#include <QFileInfo>
#include <QSettings>
#include <QApplication>
#include <QMessageBox>
#include <QPushButton>

#ifdef Q_WS_WIN
#include "../src/integration/win_functions.h"
#endif

void deleteSettings();
bool confirmDeleteSettings();
bool handleCmdLineParams(bool *const silent, bool *const tempUnsetAutostartOnly);
void tempUnsetAutostart();

int main(int argc, char*argv[])
{
    // Initialize QApplication
    QApplication app(argc, argv);
    app.setApplicationName(
        QCoreApplication::translate("main", "Notifier settings cleaner",
                                    "application's name (window title)") );
    app.setWindowIcon(QIcon(":/grnotifqt.svg"));

    // Handle command line arguments
    bool silent = false;
    bool tempUnsetAutostartOnly = false;
    if ( ! handleCmdLineParams(&silent, &tempUnsetAutostartOnly) )
    {
        QMessageBox::critical(0, qApp->applicationName(),
            QCoreApplication::translate("main", "An invalid or unknown parameter "
            "was passed by a command line argument. Will exit now without doing "
            "anything."));
        return 0;
    }

    // if just unset autostart temporarely (won't delete settings)
    if (tempUnsetAutostartOnly)
    {
        tempUnsetAutostart();
        return 0;
    }

    if (silent) // if silent
    {
        deleteSettings();
    }
    else // if not silent
    {  
        if (confirmDeleteSettings() ) // shows a confirmation dialog
        {
            deleteSettings();
            QMessageBox::information(0, qApp->applicationName(),
                                     QCoreApplication::translate("main",
                                     "Settings deleted successfully.") );
        }
    }
    return 0;
}

/**
This deletes the application's settings from the current user
*/
void deleteSettings()
{
    QSettings settings("Lasse Liehu", "Google Reader Notifier Qt");
    settings.clear();
#ifdef Q_WS_WIN
    notifier::unsetAutostartForApp("Google Reader Notifier Qt");
#endif
}

/**
This shows a graphical confirmation to the user: to delete or not to delete
the settings
@returns true if user wants to delete the settings, false otherwise
*/
bool confirmDeleteSettings()
{
    QMessageBox confirmBox;
    confirmBox.setWindowTitle(qApp->applicationName() );
    confirmBox.setText( QMessageBox::tr("Do you really want to delete all "
                        "Google\nReader Notifier Qt settings from this user?"));
    confirmBox.setIcon(QMessageBox::Question);
    QAbstractButton *deleteButton =
            confirmBox.addButton(QAbstractButton::tr("Delete settings"),
                                 QMessageBox::AcceptRole);
    QAbstractButton *cancelButton =
            confirmBox.addButton(QAbstractButton::tr("Cancel"),
                                 QMessageBox::RejectRole);
    confirmBox.setEscapeButton(cancelButton);
    confirmBox.exec();
    if (confirmBox.clickedButton() == deleteButton)
    {
        return true;
    }
    else return false;
}

/**
Handles command line parameters.
@returns True if succeeded, false if unknown or invalid parameters were found.
*/
bool handleCmdLineParams(bool *const silent, bool *const tempUnsetAutostartOnly)
{
    QStringList params = QCoreApplication::arguments();
    QStringListIterator i(params);
    bool failed = false;
    if (i.hasNext() && i.peekNext().contains(
        QFileInfo(QCoreApplication::applicationFilePath()).fileName() ))
    {
        i.next();
    }
    while (i.hasNext() )
    {
        if (i.peekNext() == "--silent") *silent = true;
        else if (i.peekNext() == "--temp-unset-autostart-only") *tempUnsetAutostartOnly = true;
        else
        {
            failed = true;
            break;
        }
        i.next();
    }
    if (failed) return false;
    else return true;
}

void tempUnsetAutostart()
{
    QSettings settings("Lasse Liehu", "Google Reader Notifier Qt");
    settings.setValue("set_autostart_on", true);
#ifdef Q_WS_WIN
    notifier::unsetAutostartForApp("Google Reader Notifier Qt");
#endif
}
